﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;
using System.Reflection;
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC
{
    public static class PerceptionAndView_RegisterTypes
    {
        public static Type[] RegisterTypes()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            DLLContents contents = TypeProvider.GetInstance().GetDLLContents(assembly.FullName);
            var types = assembly.GetTypes();
            types.ForEach(type =>
            {
                if (type.IsTypeOkay())
                    TypeProvider.GetInstance().AddType(contents, type);
            });
            return types;
        }
    }

    public class Event : IEvent, IHasPropertyData
    {
        public Event() { }
        public Event(object eventData, ViewType viewType)
        {
            EventData = eventData;
            ViewType = viewType;
        }

        public object EventData { get; set; }

        [DataMember]
        public PropertyData _properties;

        protected ViewType _viewType;

        public ViewType ViewType { get { return _viewType; } set { _viewType = value; } }

#if NO_SILVERLIGHT
[Browsable(true)]
[TypeConverter(typeof(ExpandableObjectConverter))]
#endif
        public PropertyData ThingProperties
        {
            get
            {
                if (_properties == null)
                    Interlocked.CompareExchange<PropertyData>(ref _properties, new PropertyData(), null);

                return _properties;
            }
            set
            {
                if (!_properties.AreEqual(value))
                {
                    _properties = value;
                }
            }
        }

        public void AddOrSetProperty(string name, object o)
        {
            ThingProperties.Props.AddOrSet(name, new IdentifierAndValue() { Identifier = name, Value = o });
        }

        public T GetProperty<T>(string name)
        {
            IdentifierAndValue o;

            if (!ThingProperties.Props.TryGetValue(name, out o))
                return default(T);

            return (T)o.Value;
        }
    }
}